package com.zzyl.nursing.member.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.member.domain.Reservation;
import com.zzyl.nursing.member.dto.ReservationPageDto;
import com.zzyl.nursing.member.mapper.MemberReservationMapper;
import com.zzyl.nursing.member.service.MemberReservationService;
import com.zzyl.nursing.member.vo.AppointmentVo;
import com.zzyl.nursing.member.vo.MemberProjectVo;
import com.zzyl.nursing.member.vo.ReservationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MemberReservationServiceImpl implements MemberReservationService {

    @Autowired
    private MemberReservationMapper memberReservationMapper;

    /**
     * 查询已取消预约人数
     *
     * @return
     */
    @Override
    public long cancelledCount() {
        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<Reservation>().eq(Reservation::getStatus, 2);
        return memberReservationMapper.selectCount(wrapper);
    }


    /**
     * 查询预约时间段内预约人数
     *
     * @param time
     * @return
     */
    @Override
    public List<AppointmentVo> countByTime(long time) {
        //解析时间戳
        Date date = DateUtil.date(time);
        //log.info("解析时间戳，当前时间为：{}", date);
        //
        //LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<>();
        //wrapper.ge(Reservation::getTime, date);

        Date startTime = DateUtil.beginOfDay(date);          // 当天 00:00:00
        Date endTime = DateUtil.endOfDay(date); // 次日 00:00:00（不包含）

        List<AppointmentVo> appointmentVos = memberReservationMapper.selectInsideTime(startTime, endTime);
        appointmentVos.stream().forEach(appointmentVo -> {
            Integer count = appointmentVo.getCount();
            count = 6 - count;
            appointmentVo.setCount(count);
            Date voTime = appointmentVo.getTime();

        });

        return appointmentVos;
    }

    /**
     * 预约
     *
     * @param reservation
     */
    @Override
    public void reservation(Reservation reservation) {
        //判断相同手机号在相同时间段是否存在预约
        String mobile = reservation.getMobile();
        Date time = reservation.getTime();

        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<Reservation>().eq(Reservation::getMobile, mobile).eq(Reservation::getTime, time);
        Reservation reservationReturn = memberReservationMapper.selectOne(wrapper);
        if (ObjectUtil.isNotEmpty(reservationReturn)) {
            throw new BaseException("该手机号在相同时间段已存在预约");
        }

        Long userId = UserThreadLocal.get();

        Date now = new Date();

        reservation.setCreateTime(now);
        reservation.setUpdateTime(now);
        reservation.setUserId(userId);
        reservation.setStatus(0);

        memberReservationMapper.insert(reservation);
    }

    /**
     * 分页查询预约记录
     *
     * @param reservation
     * @return
     */
    @Override
    public ReservationVo page(ReservationPageDto reservation) {
        Integer pageNum = reservation.getPageNum();
        Integer pageSize = reservation.getPageSize();
        Integer status = reservation.getStatus();

        Page<Reservation> reservationPage = new Page<>(pageNum, pageSize);

        Long userId = UserThreadLocal.get();

        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<Reservation>().eq(status != null, Reservation::getStatus, status).eq(Reservation::getUserId, userId);
        Page<Reservation> page = memberReservationMapper.selectPage(reservationPage, wrapper);
        return new ReservationVo(page.getTotal(), page.getRecords());
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void cancel(Integer id) {
        Reservation reservation = memberReservationMapper.selectById(id);
        reservation.setStatus(2);
        memberReservationMapper.updateById(reservation);
    }
}
